package com.example.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bookstore.common.PageResult;
import com.example.bookstore.common.Result;
import com.example.bookstore.entity.Book;
import com.example.bookstore.entity.User;
import com.example.bookstore.mapper.UserMapper;
import com.example.bookstore.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public PageResult<User> selectByPageAndCondition(int currentPage, int pageSize, User user) {
        Page<User> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(user.getGender()),User::getGender,user.getGender());
        lqw.like(StringUtils.isNotEmpty(user.getUsername()),User::getUsername,user.getUsername());

        Page<User> pages = userMapper.selectPage(page, lqw);
        List<User> records = pages.getRecords();
        long total = pages.getTotal();
        PageResult<User> pageResult=new PageResult<>(records,total,currentPage,pageSize);

        return pageResult;
    }

    @Override
    public User selectByUsername(String username,int id) {
        if (id==0) {//添加逻辑
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getUsername, username);
            User user = userMapper.selectOne(lqw);
            return user;
        }else{//修改逻辑
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getUsername, username);
            User user = userMapper.selectOne(lqw);
            if (user==null){
                return null;
            }
            if (user.getId()==id){
                return null;//用户名可用
            }
            return user;//该用户已存在
        }
    }

    @Override
    public Result<String> addUser(User user) {
        user.setUserImg("http://localhost:8080/image/"+user.getUserImg());
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return Result.success("新增成功");
        }
        return Result.error("新增失败");
    }

    @Override
    public User updateUser(User user) {
        int insert = userMapper.updateById(user);
        if (insert > 0) {
            return  user;
        }
        return null;
    }
    @Override
    public User updateAvatar(User user) {
        user.setUserImg("http://localhost:8080/image/"+user.getUserImg());
        int insert = userMapper.updateById(user);
        if (insert > 0) {
            return  user;
        }
        return null;
    }

    @Override
    public Result<String> enable(Integer id) {
        User user = userMapper.selectById(id);
        user.setStatus(1);
        userMapper.updateById(user);
        return Result.success("已启用");
    }

    @Override
    public Result<String> disable(Integer id) {
        User user = userMapper.selectById(id);
        user.setStatus(0);
        userMapper.updateById(user);
        return Result.success("已禁用");
    }

    @Override
    public Result<String> deleteUser(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            long id = ids.get(i);
            int del = userMapper.deleteById(id);
            if (del < 1) {
                return Result.error("删除失败");
            }
        }
        return Result.success("删除成功");
    }

    @Override
    public User login(String username, String password) {
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username);
        User user = userMapper.selectOne(lqw);
        if (user.getPassword().equals(password)){
            return user;
        }
        return null;
    }

    @Override
    public User getUser(long id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public Result<String> register(Map<String, String> param) {
        String username = param.get("username");
        String password = param.get("password");
        String password1 = param.get("password1");
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        User user = userMapper.selectOne(lqw);
        if (user!=null){
            return Result.error("该用户名已存在");
        }
        if (password.equals(password1)){
            User user1 = new User();
            user1.setUsername(username);
            user1.setPassword(password);
            user1.setStatus(1);
            user1.setRealName("用户"+username);
            userMapper.insert(user1);
            return Result.success("注册成功");
        }
        return Result.error("两次密码不一致");
    }



}